﻿namespace Leetcode.N0034;
public class Solution
{
    public int[] SearchRange(int[] nums, int target)
    {
        if (nums.Length == 0)
        {
            return new[] { -1, -1 };
        }
        var left = this.Search(nums, 0, nums.Length - 1, target);
        if (left == nums.Length || nums[left] != target)
        {
            return new[] { -1, -1 };
        }
        var right = this.Search(nums, left + 1, nums.Length - 1, target + 1);
        if (right == nums.Length || nums[right] != target)
        {
            right--;
        }
        return new[] { left, right };
    }

    private int Search(int[] nums, int from, int to, int target)
    {
        while (from <= to)
        {
            var i = from + (to - from) / 2;
            if (nums[i] < target)
            {
                from = i + 1;
            }
            else
            {
                to = i - 1;
            }
        }
        return from;
    }
}